package com.example.orderapplication

import android.Manifest
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.os.Environment
import android.provider.Settings
import android.util.Log
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import android.widget.Toast
import androidx.annotation.Nullable
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import com.example.orderapplication.bean.UserData
import com.example.orderapplication.helper.DataHelper
import java.util.*
import kotlin.collections.ArrayList


class LoginActivity : AppCompatActivity() {
    private val REQUEST_CODE = 1024
    val TAG = "LoginActivity"
    private val mEtAccountNumber: EditText by lazy { findViewById(R.id.et_account_number) }
    private val mEtPassword: EditText by lazy { findViewById(R.id.et_password) }
    private val mTvRegister: TextView by lazy { findViewById(R.id.tv_register) }
    private val mBtSubmit: Button by lazy { findViewById(R.id.bt_submit) }
    private var DB: DataHelper? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_login)
        requestPermission()
        DB = DataHelper(this)
        Log.e(TAG, DB.toString())
        mTvRegister.setOnClickListener {
            startActivity(
                Intent(
                    this@LoginActivity, RegisterActivity::class.java
                )
            )
        }
        mBtSubmit.setOnClickListener {
            val account = mEtAccountNumber.text.toString()
            val password = mEtPassword.text.toString()
            if (account.isEmpty()) {
                Toast.makeText(this, "请输入账号", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }
            if (password.isEmpty()) {
                Toast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }
            val filmDataArrayList: ArrayList<UserData> = DB!!.queryUser(account)

            if (filmDataArrayList.find {
                    if (it.phone == account) {
                        it.password == password
                    } else {
                        false
                    }
                } == null) {
                Toast.makeText(this, "登录失败请检查账号信息", Toast.LENGTH_SHORT).show()
            } else {
                Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show()
                //关闭界面跳转到主页
                finish()
                startActivity(Intent(this, MainActivity::class.java))
            }
        }

    }

    private fun requestPermission() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            if (!Environment.isExternalStorageManager()) {
                val intent = Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION)
                intent.data = Uri.parse("package:$packageName")
                startActivityForResult(intent, REQUEST_CODE)
            }
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (ActivityCompat.checkSelfPermission(
                    this,
                    Manifest.permission.READ_EXTERNAL_STORAGE
                ) == PackageManager.PERMISSION_GRANTED &&
                ContextCompat.checkSelfPermission(
                    this,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE
                ) == PackageManager.PERMISSION_GRANTED
            ) {
            } else {
                ActivityCompat.requestPermissions(
                    this,
                    arrayOf(
                        Manifest.permission.READ_EXTERNAL_STORAGE,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE
                    ),
                    REQUEST_CODE
                )
            }
        }
    }

    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<String?>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        if (requestCode == REQUEST_CODE) {
            if (ActivityCompat.checkSelfPermission(
                    this,
                    Manifest.permission.READ_EXTERNAL_STORAGE
                ) == PackageManager.PERMISSION_GRANTED &&
                ContextCompat.checkSelfPermission(
                    this,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE
                ) == PackageManager.PERMISSION_GRANTED
            ) {
                Toast.makeText(this, "存储权限获取成功", Toast.LENGTH_SHORT).show()
            } else {
                Toast.makeText(this, "存储权限获取失败", Toast.LENGTH_SHORT).show()
            }
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, @Nullable data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == REQUEST_CODE && Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            if (Environment.isExternalStorageManager()) {
                Toast.makeText(this, "存储权限获取成功", Toast.LENGTH_SHORT).show()
            } else {
                Toast.makeText(this, "存储权限获取失败", Toast.LENGTH_SHORT).show()
            }
        }
    }
}